// pages/picture/picture.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    titleList: [],
    titleList1: [],
    manList:{},
    active: 0,
    tagList:[],
    leftList:[],
    rightList:[],
  },

  goback() {
    wx.redirectTo({
      url: "../index/index",
    });
  },
  showImageFullScreen(event){ 
    let arr = []
    arr.push(event.target.dataset.parmas)
    wx.previewImage({
      urls: arr,//imgUrl 必须是需要预览的图片链接列表，只有一张图片也需要是列表
      success:(res=>{
        console.log('接口调用成功',res)
      })
    })
  },
  // goPictureDetails(event){
  //   const getId = event.currentTarget.dataset.item.id
  //   console.log(getId);
  //   wx.redirectTo({
  //     url: '../pictureDetails/pictureDetails?id=' + getId,
  //   });
  // },
  /**
   * 生命周期函数--监听页面加载
   */

  onLoad(options) {
    const that = this;
    that.getpicturemanList().then(() => {
      const photographer = that.data.titleList.find(item => item.name === options.name);
      if (photographer) {
        // 请求特定摄影师的作品列表
        that.getMediaPageByPhotographerId(photographer.id);
      }
      that.setData({
        active: photographer.id,
      })
      this.onChange({ detail: { name: photographer.id } });
    });
  },
  getpicturemanList() {
    const that = this; // 保存对当前页面实例的引用
    return new Promise((resolve) => {
      wx.request({
        url: 'https://yingshi.saas.youyougoo.com/app-user-api/yingshi/photographer/page', // 修正了 URL
        method: 'GET',
        header: {
          "tenant-id": "155",
          "Authorization": "Bearer test131",
        },
        data: {
          pageNo: 1,
          pageSize: 100,
        },
        success: (res) => {
          let currentTitleList = [];
          res.data.data.list.forEach(item => {
            if (!currentTitleList.includes(item.name)) {
              currentTitleList.push(item);
            }
          });
          that.setData({
            titleList: currentTitleList,
            titleListWithIds: res.data.data.list,
          });
          resolve();
        },
        fail: (err) => {
          console.error(err);
          resolve();
        }
      });
    });
  },
  onChange(event) {
    console.log(event);
    const currentId = event.detail.name;
    wx.request({
      url: `https://yingshi.saas.youyougoo.com/app-user-api/yingshi/photographer/get?id=${currentId}`, 
      method: 'GET',
      header: {
        "tenant-id": "155",
        "Authorization": "Bearer test131",
      },
      success: (res) => {
        // 使用 this.setData() 更新页面数据
        this.setData({// 假设res.data是你要更新的数据
          manList: res.data.data,
          name: currentId
        })
        this.getMediaPageByPhotographerId(currentId);
      },
      fail: (err) => {
        console.error(err);
      }
    })
      this.getMediaPageByPhotographerId(currentId);
      wx.request({
        url: 'https://yingshi.saas.youyougoo.com/app-user-api/yingshi/media/getMediaPage', 
        method: 'POST',
        header: {
          "tenant-id": "155",
          "Authorization": "Bearer test131",
        },
        data:{
          pageNo:1,
          pageSize:100,
          photographerId: currentId
        },
        success: (res) => {
          const imageList = res.data.data.list.map(item => {
            return {
              content:item.description,
              src:item.fileUrl,
              id:item.id,
              type:item.mediaType
            }
          })
          // 使用 this.setData() 更新页面数据
          this.setData({// 假设res.data是你要更新的数据
            titleList1: imageList
          })
          this.splitTitleList1()
        },
        fail: (err) => {
          console.error(err);
        }
      })
  },
  getMediaPageByPhotographerId(photographerId) {
    const that = this;
    wx.request({
      url: 'https://yingshi.saas.youyougoo.com/app-user-api/yingshi/media/getMediaPage',
      method: 'POST',
      header: {
        "tenant-id": "155",
        "Authorization": "Bearer test131",
      },
      data: {
        pageNo: 1,
        pageSize: 100,
        photographerId: photographerId,
      },
      success: (res) => {
        // ... 更新页面数据 ...
        that.setData({
          titleList1: res.data.data.list.map(item => ({
            content: item.description,
            src: item.fileUrl,
            id: item.id,
            type:item.mediaType
          })),
        });
        console.log('找找',this.data.titleList1);
        this.splitTitleList1()
      },
    })
  },
  // 假设您已经在某个合适的时机（例如在获取到 titleList1 后）调用此方法
splitTitleList1() {
  // 创建两个空数组
  let arr1 = []; // 用于存储索引为奇数的元素
  let arr2 = []; // 用于存储索引为偶数的元素

  // 遍历 titleList1
  for (let i = 0; i < this.data.titleList1.length; i++) {
    if (i % 2 === 0) {
      // 如果索引是偶数，添加到 arr2
      arr2.push(this.data.titleList1[i]);
    } else {
      // 如果索引是奇数，添加到 arr1
      arr1.push(this.data.titleList1[i]);
    }
  }

  // 使用 setData 更新页面数据
  this.setData({
    leftList: arr1, // 假设您想将 arr1 赋值给 letfList
    rightList: arr2, // 假设您想将 arr2 赋值给 rightList
  });
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},
  
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
